package mysql

import (
	"database/sql"
	"go-0650-blog-grom/models"
	"go.uber.org/zap"
)

type CommunityMapper struct {
}

/**
 * @Author SSWANG
 * @Description //TODO 查询分类社区列表
 * @Date 20:54 2022/10/28
 **/
func (con CommunityMapper) GetCommunityList() (communityList []*models.Community, err error) {
	communityList = []*models.Community{}

	err = Db.Find(communityList).Error

	if err == sql.ErrNoRows { // 查询为空
		zap.L().Warn("there is no community in db")
		err = nil
	}
	return
}

func (con CommunityMapper) GetCommunityNameByID(idStr string) (community *models.Community, err error) {
	community = new(models.Community)

	err = Db.Where("community_id = ?", idStr).Find(&community).Error

	if err != nil {
		zap.L().Error("query community failed", zap.Error(err))
		err = ErrorQueryFailed
		return
	}
	return
}

/**
 * @Author SSWANG
 * @Description //TODO 根据ID查询分类社区详情
 * @Date 19:57 2022/10/28
 **/
func (con CommunityMapper) GetCommunityByID(id uint64) (community *models.CommunityDetail, err error) {
	community = new(models.CommunityDetail)

	err = Db.Where("community_id = ?", id).Find(community).Error

	if err != nil {
		zap.L().Error("query community failed", zap.Error(err))
		err = ErrorQueryFailed
	}
	return
}
